package com.mybatis.entity;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Date 2019/11/13
 * 订单表
 **/
public class Order {
    /** 订单编号 **/
    private Long orderNo;
    /** 总价 **/
    private BigDecimal amount;
    /** 购买者id **/
    private Long buyerId;
    /** 商家ID **/
    private Long merchantId;

    public Order(Long orderNo,BigDecimal amount,Long buyerId,Long merchantId){
         this.orderNo=orderNo;
         this.amount=amount;
         this.buyerId=buyerId;
         this.merchantId=merchantId;
    }

    public Long getOrderNo() {
        return orderNo;
    }

    public void setOrderNo(Long orderNo) {
        this.orderNo = orderNo;
    }

    public BigDecimal getAmount() {
        return amount;
    }

    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }

    public Long getBuyerId() {
        return buyerId;
    }

    public void setBuyerId(Long buyerId) {
        this.buyerId = buyerId;
    }

    public Long getMerchantId() {
        return merchantId;
    }

    public void setMerchantId(Long merchantId) {
        this.merchantId = merchantId;
    }

    public static void main(String[] args) {
        List<Order> list= new ArrayList<>();
        Order order1 =new Order(333L,new BigDecimal(88),3L,33L);
        Order order2 =new Order(444L,new BigDecimal(99),4L,44L);
        Order order3 =new Order(555L,new BigDecimal(199),5L,55L);
        list.add(order1);
        list.add(order2);
        list.add(order3);

        //根据商家分组
        Map<Long, List<Order>> groupByMerchant = list.stream().collect(Collectors.groupingBy(Order::getMerchantId));
        for(Long merchantId:groupByMerchant.keySet()){
            System.out.println("商家id"+merchantId);
        }
        //从list中筛选55商家的订单
        List<Order> merchantOrder = list.stream().filter(a -> a.getMerchantId().equals(55L)).collect(Collectors.toList());
        System.out.println("订单数量："+merchantOrder.size()+",第一个订单金额："+merchantOrder.get(0).getAmount());
        //求所有订单总金额
        BigDecimal total= list.stream().map(Order::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        System.out.println("求和总金额："+total);

        //金额最小订单值
        BigDecimal minAmount = list.stream().map(Order::getAmount).min(BigDecimal::compareTo).get();
        //金额最大订单值
        BigDecimal maxAmount = list.stream().map(Order::getAmount).max(BigDecimal::compareTo).get();
        //金额最大订单对象
        Comparator<Order> comparator = Comparator.comparing(Order::getAmount);
        Order maxOrder = list.stream().max(comparator).get();
        System.out.println("最小："+minAmount+",最大："+maxAmount+"，最大订单编号："+maxOrder.getOrderNo());

        //用购买者id转map
        Map<Long, Order> userOrder = list.stream().collect(Collectors.toMap(Order::getBuyerId, a -> a,(k1,k2)->k1));

        //单字段排序，根据订单金额排序
        list.sort(Comparator.comparing(Order::getAmount));
        //多字段排序，根据订单金额和订单编号排序
        list.sort(Comparator.comparing(Order::getAmount).thenComparing(Order::getOrderNo));

        //注意这里需要重写order对象的equals和hashcode方法
        List<Order> distinctOrder = list.stream().distinct().collect(Collectors.toList());
        //批量将商家都改为99
        list.stream().forEach(a -> a.setMerchantId(99L));

        //获取buyerId的列表
        List<Long> buyerIdList = list.stream().map(a -> a.getBuyerId()).collect(Collectors.toList());


    }
}
